﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour 
{	
	private Animator anim;
	public Collider2D box;

	public float velocity;
	public float jump;
	float jumpOriginal;
	public float up_x;
	public float up_y;

	public int lifes;
	public int points;
	public float timeBase;
	float time;

	float playerInicioX;
	float playerInicioY;

	public int andar;
	public bool UpStairsRight;
	public bool UpStairsLeft;
	public bool enterElev;

	public GUIStyle customButton;

	public GameObject policial;

	public GameObject objetivo1;
	public GameObject objetivo2;
	public GameObject objetivo3;
	public GameObject objetivo4;
	public GameObject objetivo5;
	public GameObject objetivo6;
	public GameObject objetivo7;
	public int Sacos;


	// Use this for initialization
	void Start ()  
	{
		box =  gameObject.GetComponent<BoxCollider2D>();
		anim = gameObject.GetComponent<Animator>();
		time = timeBase;
		jumpOriginal = jump;
		playerInicioX = gameObject.transform.position.x;
		playerInicioY = gameObject.transform.position.y;
	}
	
	// Update is called once per frame
void Update () 
	{	//Aqui são ativadas todas as animações que são chamadas lá no Unity
		//aqui faço o player se mover com o A,S,D
		timeBase -= Time.deltaTime;
		if (time - timeBase > 1)
		{
			time -= 1;
		}

		if (time == 0)
		{
			Application.LoadLevel("Game Over");
		}
		if(Input.GetKey(KeyCode.S) && !UpStairsRight && !UpStairsLeft && !enterElev)
		{
			anim.SetTrigger("DireitaBaixo");
			anim.SetTrigger("EsquerdaBaixo");


		}
		if(Input.GetKey(KeyCode.A) && !UpStairsRight && !UpStairsLeft && !enterElev)
		{
			transform.Translate(-velocity * Time.deltaTime, 0, 0);

			anim.SetTrigger("LadraoEsquerda");
		}
		//else{

		//anim.SetTrigger("LadraoStop");

		//}

		if(Input.GetKey(KeyCode.D) && !UpStairsRight && !UpStairsLeft && !enterElev)
		{
			transform.Translate(velocity * Time.deltaTime, 0, 0);

			anim.SetTrigger("LadraoDireita");
		}
		//else{
		
		//anim.SetTrigger("LadraoStop");
		
		//}

		if(!Input.anyKey)
		{
			anim.SetTrigger("LadraoStop");
		}

	//aqui estão as variaveis que fazem o player subir as escadas, com suas respectivas direções descritas
		if(UpStairsRight)
		{
			transform.Translate(up_x * Time.deltaTime, up_y * Time.deltaTime, 0);
			gameObject.rigidbody2D.gravityScale = 0;
			box.isTrigger = true;
		}
		if(UpStairsLeft)
		{
			transform.Translate((-1*up_x) * Time.deltaTime, up_y * Time.deltaTime, 0);
			gameObject.rigidbody2D.gravityScale = 0;
			box.isTrigger = true;
		}

		//desliga-se a gravidade quando o player sobe as escadas, nesse teste vejo se ele está subindo alguma escada, e caso não, ligo a fisica do jogo
		if(!UpStairsRight && !UpStairsLeft)
		{
			gameObject.rigidbody2D.gravityScale = 1;
			box.isTrigger = false;
		}
		if (lifes == 0) 
		{
			Application.LoadLevel("Game Over");
			}


	}

	
void OnCollisionEnter2D(Collision2D other)
	{
		//testando qual escada ele ira subir
		if(other.gameObject.tag == "Clip Stairs Right")
		{
			UpStairsRight = true;
			jump = 0;
		}
		if(other.gameObject.tag == "Clip Stairs Left")
		{
			UpStairsLeft = true;
			jump = 0;
		}
		/*if (other.gameObject.tag == "Perdi")
		{
			gameObject.transform.position = new Vector3(playerInicioX, playerInicioY, 0);
		}*/


	}

	void OnCollisionStay2D(Collision2D other)
	{ // aqui faço com que o player pule
		if(other.gameObject.tag == "Chao")
		{
			if(Input.GetKey(KeyCode.W) && !UpStairsRight && !UpStairsLeft && !enterElev)
			{
				transform.Translate(0,jump * Time.deltaTime, 0);
				anim.SetTrigger("PuloDireita");
				anim.SetTrigger("PuloEsquerda");
			}
		}
	}


	void OnTriggerEnter2D(Collider2D other)
	{		
		//testando se ele terminou de subir as escadas
		if(other.gameObject.tag == "Stop Clip Stairs" )
		{
			UpStairsRight = false;
			UpStairsLeft = false;
			jump = jumpOriginal;
		}
		if (other.gameObject.tag == "Perdi")
		{
			gameObject.transform.position = new Vector3(playerInicioX, playerInicioY, 0);
			policial.transform.position = new Vector3(policial.GetComponent<PlayerPolice>().policialInicioX, policial.GetComponent<PlayerPolice>().policialInicioY, 0);
			policial.GetComponent<PlayerPolice>().Avistado = false;
			lifes -= 1;
		}
		if (other.gameObject.tag == "Ametista") 
		{
			if(other.gameObject.GetComponent<SpriteRenderer>().enabled == true)
			{
				points += 1000;
			}
			other.gameObject.GetComponent<SpriteRenderer>().enabled = false;
			objetivo1.GetComponent<SpriteRenderer>().enabled = true;
		}
		if (other.gameObject.tag == "Vaso") 
		{
			if(other.gameObject.GetComponent<SpriteRenderer>().enabled == true)
			{
				points += 1000;
			}
			other.gameObject.GetComponent<SpriteRenderer>().enabled = false;
			objetivo2.GetComponent<SpriteRenderer>().enabled = true;
		}
		if (other.gameObject.tag == "Dinheiro") 
		{
			if(other.gameObject.GetComponent<SpriteRenderer>().enabled == true)
			{
				points += 500;
				Sacos += 1;
			}
			other.gameObject.GetComponent<SpriteRenderer>().enabled = false;
			if (Sacos == 1)
			{
				objetivo3.GetComponent<SpriteRenderer>().enabled = true;
			}
			if (Sacos == 2)
			{
				objetivo4.GetComponent<SpriteRenderer>().enabled = true;
			}
			if (Sacos == 3)
			{
				objetivo5.GetComponent<SpriteRenderer>().enabled = true;
			}
			if (Sacos == 4)
			{
				objetivo6.GetComponent<SpriteRenderer>().enabled = true;
			}
			if (Sacos == 5)
			{
				objetivo7.GetComponent<SpriteRenderer>().enabled = true;
			}
		}
		if (other.gameObject.tag == "1") 
		{
			andar = 1;
		}
		if (other.gameObject.tag == "2") 
		{
			andar = 2;
		}
		if (other.gameObject.tag == "3") 
		{
			andar = 3;
		}
		if (other.gameObject.tag == "4") 
		{
			andar = 4;
		}

	}
	void OnGUI() 
	{
		GUI.Label (new Rect (30, 660, 25, 7), "Lifes: " + lifes, customButton);
		GUI.Label (new Rect (30, 685, 50, 15), "Points: " + points, customButton);
		GUI.Label (new Rect (30, 710, 50, 15), "Time: " + time, customButton);
	}

}